package com.ruoyi.app.service.impl;

import com.ruoyi.app.utils.AjaxResult;
import com.ruoyi.app.service.IUserOrderService;
import com.ruoyi.app.utils.AppUserUtils;
import com.ruoyi.common.enums.AppUserStatus;
import com.ruoyi.common.enums.OrderStatus;
import com.ruoyi.web.wuliu.domain.AppUser;
import com.ruoyi.web.wuliu.domain.MaterialMessage;
import com.ruoyi.web.wuliu.domain.UerOrderVo;
import com.ruoyi.web.wuliu.domain.UserOrder;
import com.ruoyi.web.wuliu.mapper.AppUserMapper;
import com.ruoyi.web.wuliu.mapper.MaterialMessageMapper;
import com.ruoyi.web.wuliu.mapper.UserOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserOrderService implements IUserOrderService {


    @Resource
    private MaterialMessageMapper materialMessageMapper;

    @Resource
    private AppUserMapper appUserMapper;

    @Resource
    private UserOrderMapper userOrderMapper;
    @Override
    public List<MaterialMessage> getOrder() {
        return materialMessageMapper.getEffectiveOrder();
    }

    @Override
    public MaterialMessage getOrderById(String id) {
        AppUser appUser = AppUserUtils.getAppUser();
        UserOrder userOrder = new UserOrder();
        userOrder.setUserId(appUser.getId());
        userOrder.setMaterialId(id);
        UserOrder userOrder1 = userOrderMapper.selectByUserIdAndMaterId(userOrder);
        MaterialMessage materialMessage = materialMessageMapper.selectMaterialMessageById(id);
        if (userOrder1 != null){
            materialMessage.setUserOrderStatus(userOrder1.getStatus());
            materialMessage.setLoadingPicture(userOrder1.getLoadingPicture());
            materialMessage.setUserOrderloadingTime(userOrder1.getLoadingTime());
            materialMessage.setUnloadPicture(userOrder1.getUnloadPicture());
            materialMessage.setUnloadTime(userOrder1.getUnloadTime());
        }
        return materialMessage;
    }

    @Override
    public AjaxResult orderTaking(String id) {
        AppUser appUser = AppUserUtils.getAppUser();
        AppUser user = appUserMapper.selectAppUserById(appUser.getId());
        if (AppUserStatus.NOTCERTIFIEDYET.getCode().equals(user.getStatus())){
            return AjaxResult.error("用户未认证无法进行接单服务");
        }
        if (AppUserStatus.UNDERREVIEW.getCode().equals(user.getStatus())){
            return AjaxResult.error("用户车辆信息审核中无法进行接单服务");
        }
        if (AppUserStatus.MISTAKE.getCode().equals(user.getStatus())){
            return AjaxResult.error("用户车辆信息审核不通过无法进行接单服务");
        }
        if (AppUserStatus.FROZEN.getCode().equals(user.getStatus())){
            return AjaxResult.error("用户已被冻结无法进行接单服务");
        }

        UserOrder userOrder = new UserOrder();
        userOrder.setUserId(user.getId());
        userOrder.setMaterialId(id);
        UserOrder userOrder1 = userOrderMapper.selectByUserIdAndMaterId(userOrder);
        if (userOrder1 != null){
            return AjaxResult.error("用户不能重复接单");
        }
        userOrder.setStatus(OrderStatus.AWAIT.getCode());
        userOrder.setCreateTime(new Date());
        userOrderMapper.insertUserOrder(userOrder);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult loading(UserOrder userOrder) {
        System.out.println(userOrder.getMaterialId());
        AppUser appUser = AppUserUtils.getAppUser();
        userOrder.setUserId(appUser.getId());
        System.out.println(appUser.getId());
        UserOrder userOrder1 = userOrderMapper.selectByUserIdAndMaterId(userOrder);
        Assert.notNull(userOrder,"订单不存在");
        userOrder1.setLoadingPicture(userOrder.getLoadingPicture());
        userOrder1.setLoadingTime(new Date());
        userOrder1.setStatus(OrderStatus.ONBOARD.getCode());
        userOrderMapper.updateUserOrder(userOrder1);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult unload(UserOrder userOrder) {
        System.out.println(userOrder.getMaterialId());
        AppUser appUser = AppUserUtils.getAppUser();
        userOrder.setUserId(appUser.getId());
        System.out.println(appUser.getId());
        UserOrder userOrder1 = userOrderMapper.selectByUserIdAndMaterId(userOrder);
        Assert.notNull(userOrder,"订单不存在");
        userOrder1.setUnloadPicture(userOrder.getUnloadPicture());
        userOrder1.setUnloadTime(new Date());
        userOrder1.setStatus(OrderStatus.DISCHARGED.getCode());
        userOrderMapper.updateUserOrder(userOrder1);
        return AjaxResult.success();
    }

    @Override
    public List<UerOrderVo> selectByUserId(String status) {
        AppUser appUser = AppUserUtils.getAppUser();
        UserOrder userOrder = new UserOrder();
        userOrder.setUserId(appUser.getId());
        userOrder.setStatus(status);
        List<UerOrderVo> list = userOrderMapper.selectByUserIdAndStatus(userOrder);
        return list;
    }

    @Override
    public List<MaterialMessage> recommend() {
        return materialMessageMapper.recommend();
    }
}
